define(["angular", 
        "route-config",
        "postal",
        "ieVersion",
        "angularLocale",
        "uiRoute", 
        "angularResource",
        "angularAnimate",
        "angularCookies",
        "angularSanitize",
        "bootstrapUI",
        "uislider", 
        "ngTable",
        "angularUEditor"
], function(angular, routeConfig, postal, ieVersion) {
    var app = angular.module("manage", [ "ui.router", "ngResource", "ngAnimate", "ngSanitize", "ngCookies", "ui.bootstrap", "ui.slider", "ngTable", "ng.ueditor" ]);
    
    app.config(["$locationProvider", 
                "$controllerProvider", 
                "$compileProvider",
                "$filterProvider",
                "$provide",
                "$stateProvider",
                "$urlRouterProvider",
                "$httpProvider",
                function($locationProvider, $controllerProvider, $compileProvider, $filterProvider, $provide, $stateProvider, $urlRouterProvider, $httpProvider) {
        
        app.lazy = {
            controller : $controllerProvider.register,
            directive : $compileProvider.directive,
            filter : $filterProvider.register,
            factory : $provide.factory,
            service : $provide.service
        };

        if (routeConfig.states !== undefined) {
            angular.forEach(routeConfig.states, function(route, state) {
                if (route.dependencies !== undefined && route.dependencies.length > 0) {
                    route.resolve = resolve(route.dependencies);
                }
                $stateProvider.state(state, route);
            });
        }
        if (routeConfig.defaultUrl !== undefined) {
            $urlRouterProvider.otherwise(routeConfig.defaultUrl);
        }

        $httpProvider.defaults.transformRequest = [function(data) {
            return angular.isObject(data) && String(data) !== '[object File]' ? $.param(data) : data;
        }];

        $httpProvider.interceptors.push(["$q", "$rootScope", "$timeout", function($q, $rootScope, $timeout) {

            $rootScope.loading = 0;

//            // 更新loading 的状态
            function updateLoadState(config, diff) {
                if (!config || config.url.indexOf("restful/") !== -1) {
                    $timeout(function() {
                        $rootScope.loading += diff;
                    }, 0);
                }
            }

             return {
                 "request": function(config) {
                     // IE 所有版本都需要去除GET 请求的缓存.
                     if (ieVersion && config.method === "GET" && config.url.indexOf("restful/") !== -1) {
                         config.headers["Cache-Control"] = "no-cache";
                         config.headers["If-Modified-Since"] = "0";
                     }
                     updateLoadState(config, 1);
                     return config;
                 },

                 "response": function(response) {
                     updateLoadState(response.config, -1);
                     return response;
                 },

                'responseError' : function (rejection) {
                    updateLoadState(rejection.config, -1);
                    if(rejection.status == 401) {
                        location.href = "#/login";
                    }
                    return $q.reject(rejection);
                }
            };
        }]);
    }]);
    
    app.config(['$provide', function ($provide) {
        $provide.decorator('$rootScope', ['$delegate', function ($delegate) {
            Object.defineProperty($delegate.constructor.prototype, '$bus', {
                get: function() {
                	var self = this;

                    return {
                        subscribe: function() {
                        	var sub = postal.subscribe.apply(postal, arguments);

                            self.$on('$destroy', function() {
                                sub.unsubscribe();
                            });
                            return sub;
                        },
                        channel: function() {
                        	return postal.channel.apply(postal, arguments);
                        },
                        publish: function() {
                        	postal.publish.apply(postal, arguments);
                        }
                    };
                },
                enumerable: false
            });

            return $delegate;
        }]);
    }]);
    
    app.value("BaseService", {
        restfulUrl : "restful/",
        formHeader : {
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        }
    });
    
    function resolve(dependencies) {
        return {
            deps : [ "$q", function($q) {
                var deferred = $q.defer();

                require(dependencies, function() {
                    deferred.resolve();
                });

                return deferred.promise;
            } ]
        };
    }

    return app;
});
